package org.kfive.nova.io;

import org.apache.log4j.Logger;

import java.io.*;
import java.util.ArrayList;

/*
* ObjectStream.java
* Created  by  dell
* Created on 2016/3/9 13:19
*/
public class ObjectStream {

    private static Logger LOG = Logger.getLogger(ObjectStream.class);

    public static void main(String[] args) {
        ObjectInputStream inputStream = null;
        ObjectOutputStream outputStream = null;
        try {
            File data = new File("d:/student.txt");
            if(!data.exists()){
                data.createNewFile();
                data.mkdirs();
            }
            outputStream = new ObjectOutputStream(new FileOutputStream(data));
            ArrayList<Student> students = new ArrayList<Student>();
            students.add(new Student("kfive1",21));
            students.add(new Student("kfive2",22));
            students.add(new Student("kfive3",23));
            outputStream.writeObject(students);
            outputStream.flush();
            Object object = null;
            inputStream = new ObjectInputStream(new FileInputStream(data));
            /*while ((object = inputStream.readObject())!=null){
                LOG.info(object);
            }*/
            ArrayList<Student> rstudents = (ArrayList<Student>)inputStream.readObject();
            for (Student student:rstudents){
                LOG.info(student);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
